package com.yimi.yinhepda.views.createPackage_module;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import butterknife.Bind;

import com.yimi.yinhepda.R;
import com.pda.ym.callbacks.UICallBack;
import com.yimi.yinhepda.db.dao.BuildPckScanTableDao;
import com.yimi.yinhepda.entity.request.ReleasePckRequest;
import com.yimi.yinhepda.entity.response.ReleasePckDownloadResponse;
import com.pda.ym.net.NetRequest;
import com.pda.ym.utils.Constants;
import com.pda.ym.utils.SoundVibratorManager;
import com.pda.ym.utils.StringUtils;
import com.yimi.yinhepda.base.BaseInitActivity;

/**
 * 解包出货界面
 *
 * @author 000432
 */
public class ReleasePackageOperatorActivity extends BaseInitActivity {
    /**
     * 标题栏左边
     */
    @Bind(R.id.title)
    TextView mTvTitle;
    /**
     * 标题栏中间
     */
    @Bind(R.id.num)
    TextView mTvNum;
    /**
     * 重量
     */
    @Bind(R.id.edit_package_weight)
    EditText mEditWeight;
    /**
     * 总票数
     */
    @Bind(R.id.edit_total_bill)
    EditText mEditTotalBill;
    /**
     * 目的地
     */
    @Bind(R.id.edit_destion)
    EditText mEditDestion;
    /**
     * 包号
     */
    @Bind(R.id.edit_packageno)
    EditText mEditPackageNo;
    @Bind(R.id.btn_download)
    Button mBtnDownload;
    @Bind(R.id.btn_back)
    Button mBtnBack;
    @Bind(R.id.btn_deblocking)
    Button mBtnDeblocking;
    /**
     * 扫描到的条码
     */
    private String mStrBarCode;

    public ReleasePackageOperatorActivity() {
        super(true);
    }

    @Override
    public String getPageName() {
        return getString(R.string.page_release_package);
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_releasepackage_out;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        mTvTitle.setText(getString(R.string.release_package_title));
    }

    @Override
    public void initEvent() {
        mBtnDownload.setOnClickListener(this);
        mBtnBack.setOnClickListener(this);
        mBtnDeblocking.setOnClickListener(this);
    }

    @Override
    public void initData() {
        //        mEditPackageNo.setText("B2017040100001");
    }

    /**
     * 扫描回调
     */
    @Override
    protected void handleBarCode(String result) {
        super.handleBarCode(result);
        mEditPackageNo.setText(result);
    }

    @Override
    protected void onResume() {
        super.onResume();
        /** 查询未上传数据*/
        int totalNotUploadNum = new BuildPckScanTableDao().queryNotUploadDataNum();
        mTvNum.setText(totalNotUploadNum + "");
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_download:
                startDownload();
                break;
            case R.id.btn_deblocking:
                //解包按钮
                startDeblocking();
                break;
            default:
                break;
        }
    }

    /**
     * 点击下载按钮，根据包号下载信息
     */
    public void startDownload() {

        mStrBarCode = mEditPackageNo.getText().toString().trim();
        if (!StringUtils.isPackgeNo(mStrBarCode)) {
            showToast("包号不规范!");
            return;
        }

        showPgDlg("正在加载...");

        new NetRequest().setMethod(NetRequest.Method.POST_STRING)
                .setUrl(Constants.URL_RELEASE_PACKAGE_DOWNLOAD)
                .setRequestObject(new ReleasePckRequest(mStrBarCode))
                .setResponsClazz(ReleasePckDownloadResponse.class)
                .execute(new UICallBack() {
                    @Override
                    public void onError(String msg) {
                        if (msg != null) {
                            showToast(msg);
                        } else {
                            showToast("服务器出错了!");
                        }
                        dismissPgDlg();
                        SoundVibratorManager.playSound(2);
                    }

                    @Override
                    public void onSuccess(Object obj) {
                        ReleasePckDownloadResponse response = (ReleasePckDownloadResponse) obj;
                        if (response == null || response.getData() == null) {
                            dismissPgDlg();
                            SoundVibratorManager.playSound(2);
                            if (response != null && response.getErrorMessages() != null && response.getErrorMessages().length > 0) {
                                showToast(response.getErrorMessages()[0]);
                            } else {
                                showToast("接口数据出错了!");
                            }
                            return;
                        }

                        mEditWeight.setText(response.getData().getWeight() + "");
                        mEditTotalBill.setText(response.getData().getPriceNum() + "");
                        mEditDestion.setText(response.getData().getDestDeptName() + "");

                        SoundVibratorManager.playSound(1);
                        dismissPgDlg();
                    }
                });
    }

    /**
     * 点击解包按钮, 调用解包上传接口
     */
    public void startDeblocking() {
        mStrBarCode = mEditPackageNo.getText().toString().trim();

        if (!StringUtils.isPackgeNo(mStrBarCode)) {
            SoundVibratorManager.playSound(2);
            showToast("包号不规范");
            return;
        }

        showPgDlg("正在解包...");

        new NetRequest().setMethod(NetRequest.Method.POST_STRING)
                .setUrl(Constants.URL_RELEASE_PACKAGE_UPLOAD)
                .setRequestObject(new ReleasePckRequest(mStrBarCode))
                .setResponsClazz(ReleasePckDownloadResponse.class)
                .execute(new UICallBack() {
                    @Override
                    public void onError(String msg) {
                        if (msg != null) {
                            showToast(msg);
                        } else {
                            showToast("服务器出错了!");
                        }

                        SoundVibratorManager.playSound(2);
                        dismissPgDlg();
                    }

                    @Override
                    public void onSuccess(Object obj) {
                        showToast("解包成功!");
                        SoundVibratorManager.playSound(1);
                        dismissPgDlg();
                        finish();
                    }
                });
    }
}
